package com.litchishell.feartures.main;

import com.litchishell.feartures.dao.Function;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author LitchiShell
 * @description: function--->apply
 * @create 2021-11-20
 */
public class FunctionTest {
    public static <T, R> List<R> map(List<T> list, Function<T, R> function) {
        ArrayList<R> rs = new ArrayList<>();
        for (T t :list) {
            rs.add(function.apply(t));
        }
        return rs;
    }

    public static void main(String[] args) {
        List<Integer> map = map(Arrays.asList("Bill", "Jerry", "Tom", "lisa"), (String s) -> s.length());
        map.forEach(System.out::println);
        List<Boolean> map1 = map(Arrays.asList("Bill", "Jerry", "Tom", "lisa"), (String s) -> s.isBlank());
        map1.forEach(System.out::println);

        // 函数复合 anThen、compose
        java.util.function.Function<Integer,Integer> f = x -> x+1;
        java.util.function.Function<Integer,Integer> g = x -> x*2;
        java.util.function.Function<Integer,Integer> h = f.andThen(g);//等价与数学中的g(f(x))
        System.out.println(h.apply(1));//4
        java.util.function.Function<Integer,Integer> a = x -> x+1;
        java.util.function.Function<Integer,Integer> b = x -> x*2;
        java.util.function.Function<Integer, Integer> c = a.compose(b);//等价与数学中的a(b(x))
        System.out.println(c.apply(1));//3
    }
}
